/**
 * zmn渠道管理-列表
 * 文件命名格式：zmn.模块名称.表名称.功能.js
 * @author xiangjunming
 * @since 2019/08/22 18:07
 */
layui.define(['index', 'form', 'table', 'laypage', 'zmnSelect', 'formSelects'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    let zmnSelect = layui.zmnSelect;

    // 原渠道
    zmnSelect.selectlChannels({
        channelOneSelectId: 'channelOneId1',
        channelTwoSelectId: 'originalChannelId',
    }, {
        channelOneSelectId: {
            maxCount: 100, //下拉框中的最多选项数量
            remote: {
                events: {
                    extendParams: function () {
                        return {status: 2}
                    },
                }
            },
            events: {
                changed: function (values) {
                    console.log("========channelOneId1==========");
                    console.log("已选中的值:" + values);
                }
            }
        },
        channelTwoSelectId: {
            isRadio: true,
            maxCount: 100, //下拉框中的最多选项数量
            remote: {
                events: {
                    extendParams: function () {
                        return {status: 2}
                    },
                }
            },
            events: {
                changed: function (values) {
                    console.log("========originalChannelId==========");
                    console.log("已选中的值:" + values);

                }
            }
        }
    })

    // 目标渠道
    zmnSelect.selectlChannelOnesTwos({
        channelOneSelectId: 'channelOneId2',
        channelTwoSelectId: 'targetChannelIdList',
    }, {
        channelOneSelectId: {
            maxCount: 100, //下拉框中的最多选项数量
            remote: {
                events: {
                    extendParams: function () {
                        return {status: 2}
                    },
                }
            },
            events: {
                changed: function (values) {
                    console.log("========channelOneId2==========");
                    console.log("已选中的值:" + values);
                }
            }
        },
        channelTwoSelectId: {
            maxCount: 100, //下拉框中的最多选项数量
            remote: {
                events: {
                    extendParams: function () {
                        return {status: 2}
                    },
                }
            },
            events: {
                changed: function (values) {
                    console.log("========targetChannelIdList==========");
                    console.log("已选中的值:" + values);
                }
            }
        }
    })

    // 复制类型切换
    form.on("radio(copyType)", function (obj) {
        let status = obj.value;
        console.log(status);
        switch (status) {
            case '1':
                $("#newChannelNameDiv").show();
                $("#newChannelName").attr("lay-verify", "required");
                $("#targetChannelDiv").hide();
                $("#rangeIdListDiv").hide();
                break;
            case '2':
                $("#newChannelNameDiv").hide();
                $("#newChannelName").attr("lay-verify", "");
                $("#targetChannelDiv").show();
                $("#rangeIdListDiv").show();
                break;
        }

    });

    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        var data = obj.field;
        if (zmnUtil.isEmptyNull(data.originalChannelId)) {
            return layer.msg("请选择原渠道", {"icon": 2});
        }
        if (zmnUtil.isEmptyNull(data.copyType)) {
            return layer.msg("请选择复制类型", {"icon": 2});
        }
        if (data.copyType == 2 && zmnUtil.isEmptyNull(data.targetChannelIdList)) {
            return layer.msg("请选择目标渠道", {"icon": 2});
        }
        // 复制范围
        var rangeIdList = [];
        $('#rangeIdList input[type=checkbox]:checked').each(function () {
            rangeIdList.push($(this).val());
        });
        if (data.copyType == 2 && rangeIdList.length == 0) {
            return layer.msg("请选择复制范围", {"icon": 2});
        }
        data.targetChannelIdList = data.targetChannelIdList.split(",");
        data.rangeIdList = rangeIdList;
        $.ajax({
            type: "POST", data: JSON.stringify(data),
            url: "/channel/add/copyChannel",
            contentType: "application/json", dataType: "json",
            success: function (result) {
                if (result.status == 200){
                    layer.msg("复制成功！", {"icon": 1});
                    setTimeout(function () {
                        parent.layui.admin.events.closeThisTabs();
                    }, 1000);
                } else {
                    layer.msg(result.message, {"icon": 2});
                }
            }
        });
        return false;
    });

    // 取消
    $(document).on('click', '.cancel', function () {
        parent.layui.admin.events.closeThisTabs();
    });

    //对外暴露的接口
    exports('channel_copy', {});
});